package com.chapter_3;

import java.util.Random;

/**
 * @Author: Timo
 * @Date: 2019/5/14 17:14
 * @Description: 客户端请求，表示用户向服务器发送请求
 */
public class ClientThread extends Thread{
    /**
     * 请求队列，用来存放客户端向服务器发送的每一个请求
     */
    private final RequestQueue requestQueue;
    /**
     * 随机数
     */
    private final Random random;

    public ClientThread(RequestQueue requestQueue, long seen, String name) {
        super(name);
        this.requestQueue = requestQueue;
        this.random = new Random(seen);
    }

    /**
     * 重写Thread.run方法，实现具体的业务逻辑
     */
    @Override
    public void run() {
        for (int i = 0; i < 10000; i++) {
            // 请求
            Request request = new Request("No." + i);
            System.out.println(">>>>>>>>>>"+Thread.currentThread().getName() + " request " + request);
            requestQueue.putRequest(request);
            try {
                Thread.sleep(random.nextInt(1000));
            } catch (InterruptedException e) {
                e.printStackTrace();
            }

        }
    }
}
